<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>

    <h1>UBSync: Sync example (r1)</h1>

    <canvas id="i-graph" width="600" height="200"></canvas>

    <!-- test code -->
    <script src="lib/ubsync.js"></script>
    <script>

      /**
       * NOTES!
       * 1) Now it works okay (below 3ms time difference) in this case:
       *     - new window/tab vs new window/tab
       *     - refreshed window/tab vs refreshed window/tab
       *    but still no luck with this case:
       *     - new window/tab vs refreshed window/tab
       *
       * 2) Within 3ms time difference, you will hear the "phasing" effect
       *    only if you play *multiple instances of identical sound file* from
       *    the one device. (like this example.)
       *    You won't hear this artifact if you play same audio from different devices.
       */

      var soundSource;

      // load audio files: entry point
      UBSync.loadAudioFile("data/drums.wav",

        function (buffer) {

          // load buffer first
          soundSource = UBSync.context.createBufferSource();
          soundSource.buffer = buffer;
          soundSource.loop = true;
          soundSource.connect(UBSync.context.destination);

          // then start stabilizing
          UBSync.stabilize(function () {
            // calculating event onset and offset
            var gNow = UBSync.GNow;
            var lNow = UBSync.LNow;
            var offset = gNow % soundSource.buffer.duration;
            // adding 40ms for accurate scheduling: lNow is already deprecated.
            soundSource.start(lNow + 0.040, offset + 0.040);
          }, 0.1, true);

      });

    </script>

  </body>
</html>